<template>
  <div class="single">
    <el-row>
      <el-col :span="24">
        <el-form
          :model="formModel"
          :rules="rules"
          ref="formModel"
          label-width="110px"
          class="demo-formModel"
        >

          <!-- 师德考核时间 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="师德考核时间" prop="khsj">
                <el-date-picker
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择师德考核时间"
                  v-model="formModel.khsj"
                  style="width: 15rem"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="师德考核结论" prop="khjs">
                <el-input v-model="formModel.khjs" placeholder="请输入师德考核结论" class="el_select"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="师德考核单位名称" prop="khdwmc">
                <el-input v-model="formModel.khdwmc" placeholder="请输入师德考核单位名称" class="el_select"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 荣誉级别 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="荣誉级别" prop="ryjb">
                <el-input v-model="formModel.ryjb" placeholder="请输入荣誉级别" class="el_select"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="荣誉称号" prop="rych">
                <el-input v-model="formModel.rych" placeholder="请输入荣誉称号" class="el_select"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="荣誉发生日期" prop="ryfssj">
                <el-date-picker
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择荣誉发生日期"
                  v-model="formModel.ryfssj"
                  style="width: 15rem"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 荣誉记录描述 -->
          <el-row>
            <el-col :span="24">
              <el-form-item label="荣誉记录描述">
                <el-input type="textarea" v-model="formModel.ryjlms" style="width:92%;"></el-input>
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 荣誉授予单位名称 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="荣誉授予单位名称" prop="sydwmc">
                <el-input v-model="formModel.sydwmc" placeholder="请输入荣誉授予单位名称" class="el_select"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row class="btns">
            <el-col :span="24">
              <el-form-item>
                <el-button type="primary" @click="submitForm('formModel')"
                >{{ btnname }}
                </el-button
                >
                <el-button @click="resetForm('formModel')">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'moralityinfo',
  data () {
    return {
      cityList: [
        {name: '中国', val: 0},
        {name: '香港', val: 1},
      ],
      educationList: [
        {name: '小学', val: 0},
        {name: '大学', val: 1},
      ],
      schoolList: [
        {name: 'xxx学院', val: 0},
        {name: 'xxx学院', val: 1},
      ],
      formModel: {
        jsId: '',
        khsj: '',
        khjs: '',
        khdwmc: '',
        ryjb: '',
        rych: '',
        ryfssj: '',
        ryjlms: '',
        sydwmc: ''
      },
      rules: {
        khjs: [
          {required: true, message: '请输入师德考核结论', trigger: 'blur'},
        ],
        khdwmc: [
          {required: true, message: '请输入师德考核单位名称', trigger: 'blur'},
        ],
        sydwmc: [
          {required: true, message: '请输入荣誉授予单位名称', trigger: 'blur'},
        ],
        ryjb: [
          {required: true, message: '请输入荣誉级别', trigger: 'blur'},
        ],
        rych: [
          {required: true, message: '请输入荣誉称号', trigger: 'blur'},
        ],
        khsj: [
          {
            type: 'string', required: true, message: '请选择师德考核时间', trigger: 'change',
          },
        ],
        ryfssj: [
          {
            type: 'string', required: true, message: '请选择荣誉发生日期', trigger: 'change',
          },
        ],

      },
      btnname: '添加',
      page: {
        id: '',
        op: 'add'
      },
    }
  },
  mounted () {
    this.initdata()
  },
  methods: {
    initdata () {
      var params = this.$route.params
      console.log(params)
      this.page.id = params.id
      this.page.op = params.op
      this.get_detail()
    },
    get_detail () {
      var params = {jsId: this.page.id}
      this.$http({
        url: this.$http.adornUrl('/sdxx/getJsAllSdxx'),
        method: 'get',
        params: this.$http.adornParams(params)
      }).then(({data}) => {
        console.log(data.data)
        if (data && data.code === 0) {
          if (data.data.length > 0) {
            this.page.op = 'detail'
            this.btnname = '修改'
            this.formModel = data.data[0]
          } else {
            this.page.op = 'add'
            this.btnname = '添加'
          }
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    get_formdata (config) {
      console.log(config)
      //对象转formdata格式
      const formData = new FormData()
      //循环传入的值转换formData
      Object.keys(config).forEach((key) => {
        formData.append(key, config[key])
      })
      return formData
    },

    submitForm (formName) {
      this.formModel.jsId = this.$store.state.user.jsid
      let formData = this.get_formdata(this.formModel)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.page.op == 'add') {
            url = '/sdxx/save'
          } else {
            url = '/sdxx/update'
          }
          this.$http({
            url: this.$http.adornUrl(url),
            data: formData,
            method: 'post'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.$nextTick(() => {
                    if (this.page.op == 'add') {
                      this.$refs[formName].resetFields()
                    }
                  })
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>


<style>
.single {
  padding: 0.5rem;
}

.el_select {
  width: 15rem;
}

.btns {
  padding-left: 22rem;
}
</style>

